define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shops/card/index' + location.search,
                    add_url: 'shops/card/add',
                    edit_url: 'shops/card/edit',
                    // del_url: 'shops/card/del',
                    multi_url: 'shops/card/multi',
                    import_url: 'shops/card/import',
                    table: 'shop',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),visible:false, operate: false},
                        {field: 'phone', title: '商家号码'},
                        {field: 'admin.nickname', title: '所属业务员',searchList: Config.salesmanList},
                        {field: 'user_id', title: '商家店铺', operate: false},
                        {field: 'amount', title: '制卡数量', operate: false},
                        {field: 'money', title: '制卡费用', operate: false},
                        {field: 'img', title: '卡片模板', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'sjdz', title: '收件地址', operate: false},
                        {field: 'sjhm', title: '收件号码', operate: false},
                        {field: 'publish_dt', title: '制卡时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                        buttons: [
                            {
                                name: 'edit',
                                text: '编辑',
                                icon: 'fa',
                                classname: 'btn btn-xs btn-success btn-editone',
                                url: 'shops/card/edit'
                            },
                            {
                                name: 'copy',
                                text: '复制',
                                icon: 'fa',
                                classname: 'btn btn-xs btn-info btn-dialog',
                                url: 'shops/card/copy'
                            },
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'shops/card/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'shop_name', title: '店铺名称', align: 'left'},
                        {
                            field: 'delete_dt',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'shops/card/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'shops/card/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
            $('#c-phone').change(function () {
                var user_id = $('#c-phone').val();
                $.ajax({
                    url:"/hFtNsJROTk.php/shops/card/getSalesman",
                    data: { user_id: user_id },
                    success:function(result){
                        if (result.code==1) {
                            $('#c-salesman_admin_id').html(result.data).selectpicker('refresh');
                        }
                    }
                })
            })
            $('#c-salesman_admin_id').change(function () {
                var user_id = $('#c-phone').val();
                var salesman_admin_id = $('#c-salesman_admin_id').val();
                if (!user_id || !salesman_admin_id) return false
                $.ajax({
                    url:"/hFtNsJROTk.php/shops/card/shopList",
                    data: { user_id: user_id,salesman_admin_id:salesman_admin_id },
                    success:function(result){
                        var html = '<ul class="list-group">'
                        for (var i = result.data.length - 1; i >= 0; i--) {
                            html+='<li class="list-group-item">'
                            html+=result.data[i].shop_name
                            html+='</li>'
                        }
                        html+='</ul>';
                        $('.shop-list').html(html)
                        $('#shop').show();
                    }
                })
            })
        },
        edit: function () {
            Controller.api.bindevent();
            $('#c-phone').change(function () {
                var user_id = $('#c-phone').val();
                $.ajax({
                    url:"/hFtNsJROTk.php/shops/card/getSalesman",
                    data: { user_id: user_id },
                    success:function(result){
                        if (result.code==1) {
                            $('#c-salesman_admin_id').html(result.data).selectpicker('refresh');
                        }
                    }
                })
            })
            $('#c-salesman_admin_id').change(function () {
                var user_id = $('#c-phone').val();
                var salesman_admin_id = $('#c-salesman_admin_id').val();
                if (!user_id || !salesman_admin_id) return false
                $.ajax({
                    url:"/hFtNsJROTk.php/shops/card/shopList",
                    data: { user_id: user_id,salesman_admin_id:salesman_admin_id },
                    success:function(result){
                        var html = '<ul class="list-group">'
                        for (var i = result.data.length - 1; i >= 0; i--) {
                            html+='<li class="list-group-item">'
                            html+=result.data[i].shop_name
                            html+='</li>'
                        }
                        html+='</ul>';
                        $('.shop-list').html(html)
                    }
                })
            })
        },
        copy: function () {
            Controller.api.bindevent();
            $('#c-phone').change(function () {
                var user_id = $('#c-phone').val();
                $.ajax({
                    url:"/hFtNsJROTk.php/shops/card/getSalesman",
                    data: { user_id: user_id },
                    success:function(result){
                        if (result.code==1) {
                            $('#c-salesman_admin_id').html(result.data).selectpicker('refresh');
                        }
                    }
                })
            })
            $('#c-salesman_admin_id').change(function () {
                var user_id = $('#c-phone').val();
                var salesman_admin_id = $('#c-salesman_admin_id').val();
                if (!user_id || !salesman_admin_id) return false
                $.ajax({
                    url:"/hFtNsJROTk.php/shops/card/shopList",
                    data: { user_id: user_id,salesman_admin_id:salesman_admin_id },
                    success:function(result){
                        var html = '<ul class="list-group">'
                        for (var i = result.data.length - 1; i >= 0; i--) {
                            html+='<li class="list-group-item">'
                            html+=result.data[i].shop_name
                            html+='</li>'
                        }
                        html+='</ul>';
                        $('.shop-list').html(html)
                    }
                })
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
